home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / s342q12.lha / expand.c < prev    next >
C/C++ Source or Header  |  1994-11-16  |  9KB  |  383 lines

  1. /*
  2. *                expand.c
  3. *
  4. * Expands a msg file.  V.1.2
  5. */
  6. #include "ctdl.h"    /* header file  */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13. #include <proto/exec.h>
  14. #include <dos/dos.h>
  15. #include <pragmas/dos_pragmas.h>
  16. #include "exec/memory.h"
  17. #include "exec/ports.h"
  18. #include "exec/exec.h"
  19. /*
  20. *                history
  21. *
  22. * 85Nov16 HAW  Modified for MS-DOS.
  23. * 85Apr22 HAW  Move to MS-DOS.
  24. * 84Dec09 HAW  Now merely expands a file, rather than copying & expanding.
  25. * 84Jun22 HAW  Version 1.1 created -- handles any file expansion.
  26. * 84Jun19 HAW  Created.
  27. */
  28. /*
  29. *                contents
  30. *
  31. *    copy_remainder()    Copies remainder of split msg into file
  32. *    crashout()        irrecoverable error
  33. *    first_part()        Remembers remainder of split msg
  34. *    getUtilNumber()        prompt user for a number, limited range
  35. *    getUtilString()        gets a string from the console
  36. *    main()            Main controller for this program
  37. */
  38. BPTR    Amsgfl;
  39. extern FILE *upfd;
  40. char        split_buffer[10000];
  41. extern        CONFIG cfg;
  42.  
  43. char mode[6];
  44.  
  45. unsigned copy_remainder ( unsigned offset );
  46. void crashout ( char *str );
  47. int first_part ( void );
  48. int getUtilNumber ( char *prompt , unsigned bottom , unsigned top );
  49. void getUtilString ( char *prompt , char *buf , int lim );
  50. void Expand_InitMsgBase ( void );
  51. void SopenFile ( char *filename , BPTR *fd );
  52. long myseek ( BPTR fd , long rpos , int mode );
  53. BPTR myopen ( char *name , char *mode );
  54. void myclose ( BPTR fd );
  55. long myread ( void *buff , int size , int blocks , BPTR fp );
  56. long mywrite ( void *buff , int size , int blocks , BPTR fp );
  57.  
  58. int NEUtilGetch(void);
  59. int UtilGetch(void);
  60. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  61.  
  62. /*
  63. * copy_remainder()
  64. *
  65. * This copies the remainder of the split msg onto the "end" of the file.
  66. */
  67. unsigned copy_remainder(offset)
  68. unsigned offset;
  69.   {
  70.   unsigned char *beg;
  71.   int  count, i;
  72.   beg = split_buffer;
  73.   count = 0;
  74.   printf("Transfer %d sectors to cover split msg\n",
  75.   (offset) ? offset / MSG_SECT_SIZE + 1 : 0);
  76.   while (1)
  77.     {
  78.     if (offset < MSG_SECT_SIZE) break;
  79.     crypte(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 0);
  80.     if (mywrite(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 1, Amsgfl) != 1)
  81.       {
  82.       printf("\ncopy_remainder:Error writing first set %d aborting\n",count);
  83.       exit(100);
  84.       };
  85.     count++;
  86.     offset -= MSG_SECT_SIZE;
  87.  
  88.     }
  89.   if (offset == 0) return (unsigned)count;
  90.   for (i = 0; beg[MSG_SECT_SIZE * count + i] != 255; i++) ;
  91.   for (i++; i < MSG_SECT_SIZE; i++)
  92.   beg[MSG_SECT_SIZE * count + i] = 0;
  93.   crypte(beg + MSG_SECT_SIZE * count, MSG_SECT_SIZE, 0);
  94.   if (mywrite(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 1, Amsgfl) != 1)
  95.     {
  96.     printf("\ncopy_remainder:Error writing second set %d aborting\n",count);
  97.     exit(100);
  98.     };
  99.   return (unsigned ) (count + 1);
  100.  
  101.   }
  102. /*
  103. * crashout()
  104. *
  105. * This handles the irrecoverable error.
  106. */
  107. void crashout(str)
  108. char *str;
  109.   {
  110.   printf(str);
  111.   exit(100);
  112.  
  113.   }
  114. /*
  115. * first_part()
  116. *
  117. * This remembers the remainder of first msg.
  118. */
  119. int first_part()
  120.   {
  121.   int  offset, i;
  122.   DATA_BLOCK buf;
  123.   char done;
  124.   offset = 0;
  125.   done   = FALSE;
  126.   do
  127.     {
  128.     if (myread(buf, MSG_SECT_SIZE, 1, Amsgfl) != 1)
  129.       {
  130.       printf("\nError on read first message\n");
  131.       exit(100);
  132.       };
  133.     crypte(buf, MSG_SECT_SIZE, 0);
  134.     i = 0;
  135.     while (buf[i] != 255 && i < MSG_SECT_SIZE)
  136.     split_buffer[offset++] = buf[i++];
  137.     done = !(i == MSG_SECT_SIZE);
  138.  
  139.     }
  140.   while (!done);
  141.   myseek(Amsgfl, 0, SEEK_SET);  /* rewind to begining */
  142.   return offset;
  143.  
  144.   }
  145. /*
  146. * getUtilNumber()
  147. *
  148. * This function prompts for a number in (bottom, top) range.
  149. */
  150. int getUtilNumber(prompt, bottom, top)
  151. char   *prompt;
  152. unsigned bottom;
  153. unsigned top;
  154.   {
  155.   unsigned try;
  156.   char numstring[NAMESIZE];
  157.   do
  158.     {
  159.     getUtilString(prompt, numstring, NAMESIZE);
  160.     try     = atoi(numstring);
  161.     if (try < bottom)  printf("Sorry, must be at least %d\n", bottom);
  162.     if (try > top   )  printf("Sorry, must be no more than %d\n", top);
  163.  
  164.     }
  165.   while ((try < bottom ||  try > top));
  166.   return (int) try;
  167.  
  168.   }
  169. /*
  170. * getUtilString()
  171. *
  172. * This will get a string from the user.
  173. */
  174. void getUtilString(prompt, buf, lim)
  175. char *prompt;
  176. char *buf;
  177. int  lim;       /* max # chars to read */
  178.   {
  179.   char c;
  180.   int  i;
  181.   if (strLen(prompt) > 0)
  182.     {
  183.     printf("\nEnter %s\n : ", prompt, lim);
  184.  
  185.     }
  186.   i   = 0;
  187.   while (
  188.   c = NEUtilGetch(),
  189.   c    != NEWLINE
  190.   && c    != '\r'
  191.   && i     <  lim
  192.   )
  193.     {
  194.     /* handle delete chars: */
  195.     if (c == BACKSPACE)
  196.       {
  197.       putchar(' ');
  198.       putchar(BACKSPACE);
  199.       if (i > 0) i--;
  200.       else
  201.         {
  202.         putchar(' ');
  203.         putchar(BELL);
  204.  
  205.         }
  206.  
  207.       }
  208.     else
  209.       {
  210.       putchar(c);
  211.       buf[i++] = c;
  212.  
  213.       }
  214.     if (i >= lim)
  215.       {
  216.       putchar(BELL);
  217.       putchar(BACKSPACE); i--;
  218.  
  219.       }
  220.  
  221.     }
  222.   buf[i]  = '\0';
  223.  
  224.   }
  225. /*
  226. * main()
  227. *
  228. * This is the main controller.
  229. */
  230. int  main(void);
  231. int main()
  232.   {
  233.   char     temp[MSG_SECT_SIZE];
  234.   char c;
  235.   int      i, offset,cnt;
  236.   unsigned oldsize, fudge;
  237.   long work;
  238.   strcpy(mode,"rb+");
  239.   printf("Citadel Message expansion program V3.42\n%s\n", COPYRIGHT);
  240.   printf("Have you backed up the ctdlmsg.sys file yet (y/n)? ");
  241.   c = getchar();
  242.   while( getchar() != '\n');
  243.   if ( c != 'Y' && c != 'y' )
  244.     {
  245.     printf("Please do so before proceeding.  Failure to do so may\n");
  246.     printf("result in you loosing everything...\n");
  247.     exit(1);
  248.  
  249.     }
  250.   printf("\n\nOne moment, please...\n");
  251.   cfg.weAre = UTILITY;
  252.   if (readSysTab(TRUE, TRUE))
  253.     {
  254.     if (access(LOCKFILE, 0) != -1)
  255.       {
  256.       printf("Please do not run Expand using Outside Commands.\n");
  257.       writeSysTab();
  258.       exit(1);
  259.  
  260.       }
  261.     cfg.weAre = UTILITY;
  262.     Expand_InitMsgBase();
  263.     oldsize     = cfg.maxMSector;
  264.     printf("\nOld size was %dK", cfg.maxMSector / (1024 / MSG_SECT_SIZE));
  265.     cfg.maxMSector = getUtilNumber("\nNew size (in decimal!)",
  266.     (unsigned) cfg.maxMSector/(1024/MSG_SECT_SIZE), 65535);
  267.     cfg.maxMSector *= (1024 / MSG_SECT_SIZE);
  268.     printf("\nThank you.  Working...");
  269.     offset = first_part();
  270.     writeSysTab();    /* Restore again, don't have to reconfigure */
  271.     work = oldsize;
  272.     work *= MSG_SECT_SIZE;
  273.     myseek(Amsgfl, 0,SEEK_END);    /* Get to EOF */
  274.     cnt = 0;
  275.     printf("Starting the expansion\n");
  276.     while (myread(temp, MSG_SECT_SIZE, 1, Amsgfl) == 1)
  277.       {
  278.       printf("reading %08.8d\r",++cnt);
  279.       };
  280.     printf("\n");
  281.     fudge = copy_remainder(offset);
  282.     for (i = 0; i < MSG_SECT_SIZE; i++) temp[i] = 0;
  283.     crypte(temp, MSG_SECT_SIZE, 0);
  284.     i = cfg.maxMSector - oldsize - fudge;
  285.     printf("And now %7d sectors left to initialize\n", i);
  286.  
  287.     for (; i; i--)
  288.       {
  289.       printf("%7d\r", i);
  290.       if (mywrite(temp, MSG_SECT_SIZE, 1, Amsgfl) != 1)
  291.         {
  292.         printf("****Error writing expansion sectors(%d left to do)\n",i);
  293.         exit(100);
  294.         };
  295.       };
  296.     myclose(Amsgfl);
  297.     printf("\nFinished.  Don't need to reconfigure!  But don't forget");
  298.     printf(" to change CTDLCNFG.SYS.\n ");
  299.  
  300.     }
  301.   return 0;
  302.   }
  303. /*
  304. * InitMsgBase()
  305. *
  306. * This function opens the msg base(s), inits the msg buffers.
  307. */
  308. void Expand_InitMsgBase()
  309.   {
  310.   SYS_FILE name;
  311.   makeSysName(name, "ctdlmsg.sys", &cfg.msgArea);
  312.   SopenFile(name, &Amsgfl);
  313.   InitBuffers();
  314.  
  315.   }
  316. /*
  317. * SopenFile()
  318. *
  319. * This opens one of the .sys files.   Has extra parameter for mode
  320. * which is global.
  321. */
  322. void SopenFile(char *filename, BPTR *fd)
  323.   {
  324.  
  325.   /* We use fopen here rather than safeopen for link reasons */
  326.   if ((*fd = myopen(filename, mode)) == NULL)
  327.     {
  328.     printf("?no %s", filename);
  329.     exit(SYSOP_EXIT);
  330.  
  331.     }
  332.  
  333.   }
  334.  
  335. long myseek( BPTR fd, long rpos, int mode)
  336.   {
  337.  
  338.   switch (mode )
  339.     {
  340.     case SEEK_SET: mode = OFFSET_BEGINNING;break;
  341.     case SEEK_CUR: mode = OFFSET_CURRENT  ;break;
  342.     case SEEK_END: mode = OFFSET_END      ;break;
  343.     default:
  344.     fprintf(stderr,"Error Invalid fseek mode:%d\n",mode);
  345.     return -1;
  346.     };
  347.  
  348.   if( fd == NULL )
  349.     {
  350.     fprintf(stderr, "Error NULL file pointer\n");
  351.     return -1;
  352.     };
  353.   (void)Seek( fd, rpos, mode );
  354.   return 0;
  355.   }
  356.  
  357. BPTR myopen( char *name, char *mode )
  358.   {
  359.   return Open( name, MODE_OLDFILE);
  360.   }
  361.  
  362. void myclose(BPTR fd)
  363.   {
  364.   Close(fd);
  365.   }
  366.  
  367. long  myread(void *buff, int size, int blocks, BPTR fp    )
  368.   {
  369.   long rsize, isize;
  370.   rsize = size * blocks;
  371.   isize =  Read( fp, buff, rsize  );
  372.   return (isize/size);
  373.   }
  374.  
  375.  
  376. long  mywrite(void *buff, int size, int blocks, BPTR fp    )
  377.   {
  378.   long rsize, isize;
  379.   rsize = size * blocks;
  380.   isize =  Write( fp, buff, rsize  );
  381.   return (isize/size);
  382.   }
  383.